/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * <p>Project: app2022 - T13
 * <p>Powered by webrx On 2022-02-07 10:48:44
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class T13 {
    CountDownLatch latch = new CountDownLatch(5);

    public static void main(String[] args) {
        var t = new T13();
        new Thread(t::worker, "T1").start();
        new Thread(t::worker, "T2").start();
        new Thread(t::worker, "T3").start();
        new Thread(t::worker, "T4").start();
        //收尾线程，当所有线程全部完万，最后最执行
        new Thread(t::over, "OVER").start();

        //main
        t.worker();

    }

    void worker() {
        String t = Thread.currentThread().getName();
        System.out.printf("线程：%s，启动..%n", t);
        for (int i = 0; i < 5; i++) {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.printf("%s : %d%n", t, i);
        }
        latch.countDown();
    }

    void over() {
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String t = Thread.currentThread().getName();
        System.out.printf("收尾线程：%s，启动..%n", t);
        System.out.println("所有线程结束");
    }
}
